package o2o.web.actions.shop;

import o2o.config.dao.UserDaoSql;
import com.jinyou.utils.common.AjaxResult;
import com.jinyou.utils.common.DateUtils;
import com.jinyou.utils.common.JYImageUtils;
import com.jinyou.utils.common.ValidateUtil;
import o2o.cache.shop.ShopImageTypeMemory;
import o2o.cache.user.UserInfoMemory;
import o2o.cache.user.UserOnlineImpl;
import o2o.dao.PO.PojoTables;
import o2o.dao.PO.shop.ShopImageType;
import o2o.dao.PO.shop.ShopImages;
import o2o.dao.PO.user.UserInfo;
import o2o.dao.VO.common.FileUploadInfo;
import o2o.dao.VO.shop.ShopImageTypeVO;
import o2o.web.actions.user.User;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import o2o.system.data.SYS_CONFIG_PATH;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

//店铺实景
@Controller
public class ShopImagesAction {

    //获取店铺实景
    @RequestMapping(method = RequestMethod.POST, value = "/shop/image/list")
    @ResponseBody
    public String shop_image_list(Long shopId) {

        if (ValidateUtil.isNotID(shopId)) {
            return AjaxResult.errorResult("请先选择店铺");
        }
        List<ShopImageTypeVO> list = ShopMan.getShopImages(shopId);
        if (null != list) {
            return AjaxResult.successResult(list.size(), list);
        }
        return AjaxResult.successResult(0, null);
    }


    //新增店铺实景
    @RequestMapping(method = RequestMethod.POST, value = "/shop/image/add_bak")
    @ResponseBody
    public String shop_image_add(String token, Long shopId, String name, String descs, FileUploadInfo fileUploadInfo) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        //验证是否有登录权限
        if (!User.getInstance().hasShopAuth(user.getUsername(), user.getUserType())) {
            return AjaxResult.errorResult("您没有操作店铺的权限");
        }

        if (ValidateUtil.isNotID(shopId)) {
            return AjaxResult.errorResult("请先选择店铺");
        }


        long time = DateUtils.getCurrTime();

        ShopImageType shopImageType = new ShopImageType();
        shopImageType.setShopId(shopId);
        shopImageType.setCreateUser(user.getUsername());
        shopImageType.setCreateTime(time);
        shopImageType.setDelFlag(0);
        shopImageType.setName(name);
        shopImageType.setDescs(descs);
        try {
            UserDaoSql.getInstance().insertObj(PojoTables.ShopImageType, shopImageType);
        } catch (SQLException e) {
            e.printStackTrace();
            AjaxResult.errorResult("保存失败，请稍后重试");
        }

        //存入缓存
        Long typeId = ShopImageTypeMemory.getInstance().addType(shopId, time);
        if (null == typeId) {
            return AjaxResult.errorResult("保存店家实景失败，请稍后再试");
        }

        if (null == fileUploadInfo) {
            if (!saveImage(user.getUsername(), shopId, typeId, fileUploadInfo.getImage1())) {
                return AjaxResult.errorResult("图片1保存失败,请重试");
            }
            if (!saveImage(user.getUsername(), shopId, typeId, fileUploadInfo.getImage2())) {
                return AjaxResult.errorResult("图片2保存失败,请重试");
            }
            if (!saveImage(user.getUsername(), shopId, typeId, fileUploadInfo.getImage3())) {
                return AjaxResult.errorResult("图片3保存失败,请重试");
            }
            if (!saveImage(user.getUsername(), shopId, typeId, fileUploadInfo.getImage4())) {
                return AjaxResult.errorResult("图片4保存失败,请重试");
            }
            if (!saveImage(user.getUsername(), shopId, typeId, fileUploadInfo.getImage5())) {
                return AjaxResult.errorResult("图片5保存失败,请重试");
            }
            if (!saveImage(user.getUsername(), shopId, typeId, fileUploadInfo.getImage6())) {
                return AjaxResult.errorResult("图片6保存失败,请重试");
            }
            if (!saveImage(user.getUsername(), shopId, typeId, fileUploadInfo.getImage7())) {
                return AjaxResult.errorResult("图片7保存失败,请重试");
            }
            if (!saveImage(user.getUsername(), shopId, typeId, fileUploadInfo.getImage8())) {
                return AjaxResult.errorResult("图片8保存失败,请重试");
            }
            if (!saveImage(user.getUsername(), shopId, typeId, fileUploadInfo.getImage9())) {
                return AjaxResult.errorResult("图片9保存失败,请重试");
            }
        }


        //放入缓存
        ShopImageTypeMemory.getInstance().reloadTypeById(shopId, typeId);


        return AjaxResult.successResult();
    }

    //实景图片新增
    @RequestMapping(method = RequestMethod.POST, value = "/shop/image/add")
    @ResponseBody
    public String shop_image_add2(String token, Long shopId, String name, String descs, FileUploadInfo fileUploadInfo) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        //验证是否有登录权限
        // if (!User.getInstance().hasShopAuth(user.getUsername(),user.getUserType())) {
        //     return AjaxResult.errorResult("您没有操作店铺的权限");
        // }

        if (ValidateUtil.isNotID(shopId)) {
            return AjaxResult.errorResult("请先选择店铺");
        }


        long time = DateUtils.getCurrTime();

        ShopImageType shopImageType = new ShopImageType();
        shopImageType.setShopId(shopId);
        shopImageType.setCreateUser(user.getUsername());
        shopImageType.setCreateTime(time);
        shopImageType.setDelFlag(0);
        shopImageType.setName(name);
        shopImageType.setDescs(descs);
        try {
            UserDaoSql.getInstance().insertObj(PojoTables.ShopImageType, shopImageType);
        } catch (SQLException e) {
            e.printStackTrace();
            AjaxResult.errorResult("保存失败，请稍后重试");
        }

        //存入缓存
        Long typeId = ShopImageTypeMemory.getInstance().addType(shopId, time);
        if (null == typeId) {
            return AjaxResult.errorResult("保存店家实景失败，请稍后再试");
        }

        if (null != fileUploadInfo) {
            if (!saveImage(user.getUsername(), shopId, typeId, fileUploadInfo.getImage1(), fileUploadInfo.getImageUrl1())) {
                return AjaxResult.errorResult("图片1保存失败,请重试");
            }
            if (!saveImage(user.getUsername(), shopId, typeId, fileUploadInfo.getImage2(), fileUploadInfo.getImageUrl2())) {
                return AjaxResult.errorResult("图片2保存失败,请重试");
            }
            if (!saveImage(user.getUsername(), shopId, typeId, fileUploadInfo.getImage3(), fileUploadInfo.getImageUrl3())) {
                return AjaxResult.errorResult("图片3保存失败,请重试");
            }
            if (!saveImage(user.getUsername(), shopId, typeId, fileUploadInfo.getImage4(), fileUploadInfo.getImageUrl4())) {
                return AjaxResult.errorResult("图片4保存失败,请重试");
            }
            if (!saveImage(user.getUsername(), shopId, typeId, fileUploadInfo.getImage5(), fileUploadInfo.getImageUrl5())) {
                return AjaxResult.errorResult("图片5保存失败,请重试");
            }
            if (!saveImage(user.getUsername(), shopId, typeId, fileUploadInfo.getImage6(), fileUploadInfo.getImageUrl6())) {
                return AjaxResult.errorResult("图片6保存失败,请重试");
            }
            if (!saveImage(user.getUsername(), shopId, typeId, fileUploadInfo.getImage7(), fileUploadInfo.getImageUrl7())) {
                return AjaxResult.errorResult("图片7保存失败,请重试");
            }
            if (!saveImage(user.getUsername(), shopId, typeId, fileUploadInfo.getImage8(), fileUploadInfo.getImageUrl8())) {
                return AjaxResult.errorResult("图片8保存失败,请重试");
            }
            if (!saveImage(user.getUsername(), shopId, typeId, fileUploadInfo.getImage9(), fileUploadInfo.getImageUrl9())) {
                return AjaxResult.errorResult("图片9保存失败,请重试");
            }
            //放入缓存
            ShopImageTypeMemory.getInstance().reloadTypeById(shopId, typeId);
        }


        return AjaxResult.successResult();
    }


    //删除店铺实景图片
    @RequestMapping(method = RequestMethod.POST, value = "/shop/image/delete")
    @ResponseBody
    public String shop_image_delete(String token, Long shopId, Long typeId, String imageIds) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        //验证是否有登录权限
        if (!User.getInstance().hasShopAuth(user.getUsername(), user.getUserType())) {
            return AjaxResult.errorResult("您没有操作店铺的权限");
        }

        if (ValidateUtil.isNotID(shopId)) {
            return AjaxResult.errorResult("请先选择店铺");
        }
        if (null == typeId) {
            return AjaxResult.errorResult("请先选择实景类型");
        }

        if (ValidateUtil.isNull(imageIds)) {
            return AjaxResult.errorResult("请选择要删除的图片");
        }

        StringBuilder sb = new StringBuilder();
        sb.append("update ").append(PojoTables.ShopImages).append(" set delFlag = 1,updateUser='").append(user.getUsername())
                .append("' where delFlag = 0 and shopId = ").append(shopId).append(" and typeId = ").append(typeId).append(" and id in (").append(imageIds).append(") ");
        try {
            UserDaoSql.getInstance().executeBySql(sb.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("删除失败，请稍后再试 ");
        }

        //在缓存中删除
        ShopImageTypeMemory.getInstance().reloadTypeById(shopId, typeId);

        return AjaxResult.successResult();
    }


    //新增实景类型名称
    @RequestMapping(method = RequestMethod.POST, value = "/shop/image/type/add")
    @ResponseBody
    public String shop_image_type_modify(String token, Long shopId, String name, String descs) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        //验证是否有登录权限
        if (!User.getInstance().hasShopAuth(user.getUsername(), user.getUserType())) {
            return AjaxResult.errorResult("您没有操作店铺的权限");
        }


        //验证是否有登录权限
        if (!User.getInstance().hasShopAuth(user.getUsername(), user.getUserType())) {
            return AjaxResult.errorResult("您没有操作店铺的权限");
        }

        if (ValidateUtil.isNotID(shopId)) {
            return AjaxResult.errorResult("请先选择店铺");
        }


        long time = DateUtils.getCurrTime();

        ShopImageType shopImageType = new ShopImageType();
        shopImageType.setShopId(shopId);
        shopImageType.setCreateUser(user.getUsername());
        shopImageType.setCreateTime(time);
        shopImageType.setDelFlag(0);
        shopImageType.setName(name);
        shopImageType.setDescs(descs);
        try {
            UserDaoSql.getInstance().insertObj(PojoTables.ShopImageType, shopImageType);
        } catch (SQLException e) {
            e.printStackTrace();
            AjaxResult.errorResult("保存失败，请稍后重试");
        }

        //存入缓存
        Long typeId = ShopImageTypeMemory.getInstance().addType(shopId, time);

        return AjaxResult.successResult(typeId);
    }

    //修改实景类型名称
    @RequestMapping(method = RequestMethod.POST, value = "/shop/image/type/modify")
    @ResponseBody
    public String shop_image_type_modify(String token, Long shopId, Long typeId, String name) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        //验证是否有登录权限
        if (!User.getInstance().hasShopAuth(user.getUsername(), user.getUserType())) {
            return AjaxResult.errorResult("您没有操作店铺的权限");
        }

        if (ValidateUtil.isNotID(shopId)) {
            return AjaxResult.errorResult("请先选择店铺");
        }
        if (ValidateUtil.isNotID(typeId)) {
            return AjaxResult.errorResult("请先选择实景类型");
        }
        if (ValidateUtil.isNull(name)) {
            return AjaxResult.errorResult("实景名称不能为空");
        }

        StringBuilder sb = new StringBuilder();
        sb.append("update ").append(PojoTables.ShopImageType).append(" set name = '").append(name).append("',updateUser = '")
                .append(user.getUsername()).append("' where id = ").append(typeId).append(" and delFlag = 0 and shopId = ").append(shopId);
        try {
            UserDaoSql.getInstance().executeBySql(sb.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("修改失败，请稍后再试");
        }

        //修改缓存
        ShopImageTypeMemory.getInstance().modifyType(shopId, typeId);

        return AjaxResult.successResult();
    }

    //按类型删除店铺实景
    @RequestMapping(method = RequestMethod.POST, value = "/shop/image/type/delete")
    @ResponseBody
    public String shop_image_type_delete(String token, Long shopId, String typeIds) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        //验证是否有登录权限
        if (!User.getInstance().hasShopAuth(user.getUsername(), user.getUserType())) {
            return AjaxResult.errorResult("您没有操作店铺的权限");
        }

        if (ValidateUtil.isNotID(shopId)) {
            return AjaxResult.errorResult("请先选择店铺");
        }
        if (ValidateUtil.isNull(typeIds)) {
            return AjaxResult.errorResult("请先选择实景类型");
        }

        //删除实景下的图片
        StringBuilder sbImage = new StringBuilder();
        sbImage.append("update ").append(PojoTables.ShopImages).append(" set delFlag = 1,updateUser = '").append(user.getUsername())
                .append("' where delFlag = 0 and shopId = ").append(shopId).append(" and typeId in (").append(typeIds).append(")");

        //删除实景
        StringBuilder sb = new StringBuilder();
        sb.append("update ").append(PojoTables.ShopImageType).append(" set delFlag = 1,updateUser = '").append(user.getUsername())
                .append("' where delFlag = 0 and shopId = ").append(shopId).append(" and id in (").append(typeIds).append(")");

        try {
            UserDaoSql.getInstance().executeBySql(sbImage.toString());
            UserDaoSql.getInstance().executeBySql(sb.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("删除店铺实景失败，请稍后再试");
        }

        //在缓存中删除
        ShopImageTypeMemory.getInstance().deleteType(shopId, typeIds);

        return AjaxResult.successResult();
    }


    //修改店铺实景
    @RequestMapping(method = RequestMethod.POST, value = "/shop/image/modify")
    @ResponseBody
    public String shop_image_modify(String token, Long shopId, Long typeId, String name, Integer delOriginData, FileUploadInfo fileUploadInfo) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        //验证是否有登录权限
        if (!User.getInstance().hasShopAuth(user.getUsername(), user.getUserType())) {
            return AjaxResult.errorResult("您没有操作店铺的权限");
        }

        if (ValidateUtil.isNotID(shopId)) {
            return AjaxResult.errorResult("请先选择店铺");
        }
        if (ValidateUtil.isNotID(typeId)) {
            return AjaxResult.errorResult("请先选择实景类型");
        }
        if (ValidateUtil.isNull(name)) {
            return AjaxResult.errorResult("实景名称不能为空");
        }

        StringBuilder sb = new StringBuilder();
        sb.append("update ").append(PojoTables.ShopImageType).append(" set name = '").append(name).append("',updateUser = '")
                .append(user.getUsername()).append("' where id = ").append(typeId).append(" and delFlag = 0 and shopId = ").append(shopId);
        StringBuilder delSql = new StringBuilder();
        if (delOriginData != null && delOriginData == 1) {
            delSql.append("update ").append(PojoTables.ShopImages).append(" set delFlag = 1,updateUser='").append(user.getUsername())
                    .append("' where delFlag = 0 and shopId = ").append(shopId).append(" and typeId = ").append(typeId);
        }
        try {
            if (delOriginData != null && delOriginData == 1) {
                UserDaoSql.getInstance().executeBySql(delSql.toString()); // 删除原先的商家实景图
            }
            UserDaoSql.getInstance().executeBySql(sb.toString());       // 修改分类
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("修改失败，请稍后再试");
        }

        //修改缓存
        // ShopImageTypeMemory.getInstance().modifyType(shopId, typeId);
        //在缓存中删除
        ShopImageTypeMemory.getInstance().reloadTypeById(shopId, typeId);

        if (null != fileUploadInfo) {
            if (!saveImage(user.getUsername(), shopId, typeId, fileUploadInfo.getImage1(), fileUploadInfo.getImageUrl1())) {
                return AjaxResult.errorResult("图片1保存失败,请重试");
            }
            if (!saveImage(user.getUsername(), shopId, typeId, fileUploadInfo.getImage2(), fileUploadInfo.getImageUrl2())) {
                return AjaxResult.errorResult("图片2保存失败,请重试");
            }
            if (!saveImage(user.getUsername(), shopId, typeId, fileUploadInfo.getImage3(), fileUploadInfo.getImageUrl3())) {
                return AjaxResult.errorResult("图片3保存失败,请重试");
            }
            if (!saveImage(user.getUsername(), shopId, typeId, fileUploadInfo.getImage4(), fileUploadInfo.getImageUrl4())) {
                return AjaxResult.errorResult("图片4保存失败,请重试");
            }
            if (!saveImage(user.getUsername(), shopId, typeId, fileUploadInfo.getImage5(), fileUploadInfo.getImageUrl5())) {
                return AjaxResult.errorResult("图片5保存失败,请重试");
            }
            if (!saveImage(user.getUsername(), shopId, typeId, fileUploadInfo.getImage6(), fileUploadInfo.getImageUrl6())) {
                return AjaxResult.errorResult("图片6保存失败,请重试");
            }
            if (!saveImage(user.getUsername(), shopId, typeId, fileUploadInfo.getImage7(), fileUploadInfo.getImageUrl7())) {
                return AjaxResult.errorResult("图片7保存失败,请重试");
            }
            if (!saveImage(user.getUsername(), shopId, typeId, fileUploadInfo.getImage8(), fileUploadInfo.getImageUrl8())) {
                return AjaxResult.errorResult("图片8保存失败,请重试");
            }
            if (!saveImage(user.getUsername(), shopId, typeId, fileUploadInfo.getImage9(), fileUploadInfo.getImageUrl9())) {
                return AjaxResult.errorResult("图片9保存失败,请重试");
            }
            //放入缓存
            ShopImageTypeMemory.getInstance().reloadTypeById(shopId, typeId);
        }


        return AjaxResult.successResult();
    }


    //获取不同类型的店铺图片
    @RequestMapping(method = RequestMethod.POST, value = "/shop/image/type/list")
    @ResponseBody
    public String shop_image_type_list(Long shopId, Long typeId) {
        //验证用户有效性
//        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
//        if (user == null) {
//            return AjaxResult.errorLogoutResult();
//        }

        if (ValidateUtil.isNotID(shopId)) {
            return AjaxResult.errorResult("请先选择店铺");
        }
        if (null == typeId) {
            return AjaxResult.errorResult("请先选择图片类型");
        }

        ShopImageType list = ShopImageTypeMemory.getInstance().listOfShopExtra(shopId, typeId);

        if (null != list && null != list.getImages()) {
            return AjaxResult.successResult(list.getImages().size(), list.getImages());
        }
        return AjaxResult.successResult(0, null);
    }


    //单独图片上传
    @RequestMapping(method = RequestMethod.POST, value = "/shop/image/upload")
    @ResponseBody
    public String shop_add(String token, FileUploadInfo fileUploadInfo, Integer fileType) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        if (null == fileUploadInfo) {
            return AjaxResult.errorResult("图片不允许为空");
        }

        if (null == fileUploadInfo.getImage() && ValidateUtil.isNull(fileUploadInfo.getBase64Img())) {
            return AjaxResult.errorResult("图片不允许为空.");
        }
        String filePath = null;

        String folder = SYS_CONFIG_PATH.SHOP_IMAGE;
        if (null != fileType && 0 == fileType - 1) {    // 1 广告图片
            folder = SYS_CONFIG_PATH.ADVERT_IMAGE;
        }
        // 照片
        if (fileUploadInfo.getImage() != null || StringUtils.isNotEmpty(fileUploadInfo.getBase64Img())) {
            filePath = StringUtils.isNotEmpty(fileUploadInfo.getBase64Img()) ? JYImageUtils.saveImage(folder, fileUploadInfo.getBase64Img()) : JYImageUtils.saveImage(folder, fileUploadInfo.getImage());
            if (null == filePath) {
                return AjaxResult.errorResult("存图片失败");
            }
        }

//         //照片
//         if (null != fileUploadInfo.getImage()) {
//             //存储图片
// //            filePath = JYImageUtils.saveImage(Common.SHOPIMAGE, fileUploadInfo.getImage());
//             filePath = JYImageUtils.saveImage(folder, fileUploadInfo.getImage());
//             if (ValidateUtil.isNull(filePath)) {
//                 return AjaxResult.errorResult("存储图片失败，请稍后再试");
//             }
//         }

        HashMap<String, String> map = new HashMap<String, String>();
//        map.put("imageUrl", Common.imageUrl + filePath);
        if (!ValidateUtil.isNetUrl(filePath)) {
            map.put("imageUrl", SYS_CONFIG_PATH.IMAGE_BASE_URL + filePath);
        } else {
            map.put("imageUrl", filePath);
        }
        map.put("imagePath", filePath);

        return AjaxResult.successResult(map);

    }

    // 单独存储图片
    @RequestMapping(method = RequestMethod.POST, value = "/shop/image/alone/add")
    @ResponseBody
    public String shop_image_lonly_add(String token, ShopImages info, FileUploadInfo fileUploadInfo) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        // //验证是否有登录权限
        // if (!User.getInstance().hasShopAuth(user.getUsername(),user.getUserType())) {
        //     return AjaxResult.errorResult("您没有操作的权限");
        // }

        if (ValidateUtil.isNotID(info.getShopId())) {
            return AjaxResult.errorResult("请先选择店铺");
        }

        if (null == info.getTypeId()) {
            return AjaxResult.errorResult("图片类型不能为空");
        }

        if (null == fileUploadInfo) {
            return AjaxResult.errorResult("图片不能为空");
        }
        if (!saveImageV2(user.getUsername(), info.getShopId(), info.getTypeId(), fileUploadInfo.getImage1(), info)) {
            return AjaxResult.errorResult("图片1保存失败,请重试");
        }
        if (!saveImageV2(user.getUsername(), info.getShopId(), info.getTypeId(), fileUploadInfo.getImage2(), info)) {
            return AjaxResult.errorResult("图片2保存失败,请重试");
        }
        if (!saveImageV2(user.getUsername(), info.getShopId(), info.getTypeId(), fileUploadInfo.getImage3(), info)) {
            return AjaxResult.errorResult("图片3保存失败,请重试");
        }
        if (!saveImageV2(user.getUsername(), info.getShopId(), info.getTypeId(), fileUploadInfo.getImage4(), info)) {
            return AjaxResult.errorResult("图片4保存失败,请重试");
        }
        if (!saveImageV2(user.getUsername(), info.getShopId(), info.getTypeId(), fileUploadInfo.getImage5(), info)) {
            return AjaxResult.errorResult("图片5保存失败,请重试");
        }
        if (!saveImageV2(user.getUsername(), info.getShopId(), info.getTypeId(), fileUploadInfo.getImage6(), info)) {
            return AjaxResult.errorResult("图片6保存失败,请重试");
        }
        //放入缓存
        ShopImageTypeMemory.getInstance().reloadTypeById(info.getShopId(), info.getTypeId());

        return AjaxResult.successResult();
    }


//    //修改图片信息
//    @RequestMapping(method = RequestMethod.POST, value = "/shop/image/alone/modify")
//    @ResponseBody
//    public String shop_image_alone_modify(String token, ShopImages info) {
//        //验证用户有效性
//        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
//        if (user == null) {
//            return AjaxResult.errorLogoutResult();
//        }
//
//
//        if (ValidateUtil.isNotID(info.getId())) {
//            return AjaxResult.errorResult("请先选择要编辑的");
//        }
//
//        StringBuilder sb = new StringBuilder();
//        sb.append("update ").append(PojoTables.ShopImages).append(" set extra1 = '").append(info.getExtra1()).append("', extra2 = '").append(info.getExtra2())
//                .append("', extra3 = '").append(info.getExtra3()).append("', name = '").append(info.getName()).append("', descs = '").append(info.getDescs())
//                .append("',updateUser = '").append(user.getUsername())
//                .append("' where id = ").append(info.getId()).append(" and delFlag = 0");
//        try {
//            UserDaoSql.getInstance().executeBySql(sb.toString());
//        } catch (SQLException e) {
//            e.printStackTrace();
//            return AjaxResult.errorResult("修改失败，请稍后再试");
//        }
//
//        //修改缓存
//        ShopImageTypeMemory.getInstance().modifyType(info.getShopId(),info.getTypeId());
//
//        return AjaxResult.successResult();
//    }


    //----------------------------------------------------------------------------
    //保存店铺图片
    private boolean saveImage(String username, Long shopId, Long typeId, MultipartFile file) {
        return this.saveImage(username, shopId, typeId, file, null);
    }

    private boolean saveImage(String username, Long shopId, Long typeId, MultipartFile file, String imageUrl) {
        if (null != file || ValidateUtil.isNotNull(imageUrl)) {
            long time = DateUtils.getCurrTime();
            //存储图片

            String filePath = null;
            if (file != null) {
                filePath = JYImageUtils.saveImage(SYS_CONFIG_PATH.SHOP_IMAGE, file);
            } else {
                filePath = imageUrl;
            }
            if (null == filePath) {
                return false;
            }
            ShopImages info = new ShopImages();
            info.setShopId(shopId);
            info.setTypeId(typeId);
            info.setUrl(filePath);
            info.setCreateTime(time);
            info.setCreateUser(username);
            info.setDelFlag(0);
            try {
                UserDaoSql.getInstance().insertObj(PojoTables.ShopImages, info);
            } catch (SQLException e) {
                e.printStackTrace();
                return false;
            }
        }
        return true;

    }


    //保存店铺图片
    private boolean saveImageV2(String username, Long shopId, Long typeId, MultipartFile file, ShopImages info) {
        if (null != file) {
            long time = DateUtils.getCurrTime();
            //存储图片
//            String filePath = JYImageUtils.saveImage(SHOPIMAGE, file);
            String filePath = JYImageUtils.saveImage(SYS_CONFIG_PATH.SHOP_IMAGE, file);
            if (null == filePath) {
                return false;
            }
//            ShopImages info = new ShopImages();
            info.setShopId(shopId);
            info.setTypeId(typeId);
            info.setUrl(filePath);
            info.setCreateTime(time);
            info.setCreateUser(username);
            info.setName(info.getName());
            info.setDescs(info.getDescs());
            info.setExtra1(info.getExtra1());
            info.setExtra2(info.getExtra2());
            info.setExtra3(info.getExtra3());
            info.setDelFlag(0);
            try {
                UserDaoSql.getInstance().insertObj(PojoTables.ShopImages, info);
            } catch (SQLException e) {
                e.printStackTrace();
                return false;
            }
        }

        return true;

    }
}
